<?php
namespace Models;

/**
 *
 * @author Eugene Baranov <evgeny@baranov.eu>
 *
 */
class User extends Base {
    const ACTIVE = 1;
    const HIDDEN = 0;
    const DELETE = -1;

    const ROLE_ADMIN = 10;
    const ROLE_GUEST = 0;
    const ROLE_WRITER = 1;
    const ROLE_MODERATOR = 2;

    const GENDER_MALE = 'male';
    const GENDER_FEMALE = 'female';

    protected $id;
    protected $name;
    protected $username;
    protected $email;
    protected $first_name;
    protected $last_name;
    protected $gender;
    protected $timezone;
    protected $status_id;

    protected $roles = array();
    /**
     *
     * @var \Models\Profile
     */
    protected $profile = array();

    /**
     * (non-PHPdoc)
     * @see Models.Base::toArray()
     */
    public function toArray() {
        return array_merge(parent::toArray(), array(
            'id' => $this->id,
            'name' => $this->name,
            'username' => $this->username,
            'email' => $this->email,
            'first_name' => $this->first_name,
            'last_name' => $this->last_name,
            'gender' => $this->gender,
            'timezone' => $this->timezone,
            'status_id' => $this->status_id
        ));
    }

    public function getId() {
        return $this->id;
    }

    public function getName() {
        return $this->name;
    }

    public function getFirstName() {
        return $this->first_name;
    }

    public function getLastName() {
        return $this->last_name;
    }

    public function getGender() {
        return $this->gender;
    }

    public function getTimezone() {
        return $this->timezone;
    }

    public function getEmail() {
        return $this->email;
    }

    public function isAdmin() {
        return $this->haveRole(self::ROLE_ADMIN);
    }

    public function isWriter() {
        return $this->haveRole(self::ROLE_WRITER);
    }

    public function isModerator() {
        return $this->haveRole(self::ROLE_MODERATOR);
    }

    public function haveRole($role_id) {
        foreach ($this->roles as $r) {
            if ($r['role_id'] == $role_id) {
                return true;
            }
        }
        return false;
    }

    public function getMaxRole() {
        $mr = self::ROLE_GUEST;
        foreach ($this->roles as $r) {
            $mr = max($mr, $r['role_id']);
        }
        return $mr;
    }

    public function getProfile() {
        return $this->profile;
    }
}